﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.Models;
namespace RMS.Controllers
{
    public class ProductController : Controller
    {
        RMSDataClassesDataContext db = new RMSDataClassesDataContext();
        public ActionResult Index()
        {
            User user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name);
            if ((user != null)&&(user.CompanyId!=null))
            {
                ViewData["companyId"] = user.CompanyId.Value;
            }
            ViewData["productGroups"] = db.ProductGroups.AsEnumerable();
            return View(db.Products);
        }

        public ActionResult AllProductGroups()
        {
            return View(db.ProductGroups);
        }
        public ActionResult CreateProductGroup()
        {
            return View();
        }
        [HttpPost]
        public ActionResult CreateProductGroup(ProductGroup newProductGroup)
        {
            try
            {
                db.ProductGroups.InsertOnSubmit(newProductGroup);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult DetailsProductGroup(int id)
        {
            return View(db.ProductGroups.FirstOrDefault(x => x.Id == id));
        }

        public ActionResult EditProductGroup(int id)
        {
            return View(db.ProductGroups.FirstOrDefault(x => x.Id == id));
        }
        [HttpPost]
        public ActionResult EditProductGroup(int id, FormCollection collection)
        {
            ProductGroup productGroup = db.ProductGroups.FirstOrDefault(x => x.Id == id);
            try
            {
                UpdateModel(productGroup);
                db.SubmitChanges();
                return RedirectToAction("AllProductGroups");
            }
            catch
            {
                return View(productGroup);
            }
        }
        public ActionResult Details(int id)
        {
            return View(db.Products.FirstOrDefault(x=>x.Id == id));
        }
        public ActionResult Create()
        {
            ViewData["productGroups"] = db.ProductGroups;
            return View();
        } 
        [HttpPost]
        public ActionResult Create(Product newProduct)
        {
            try
            {
                db.Products.InsertOnSubmit(newProduct);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {

                ViewData["productGroups"] = db.ProductGroups;
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            return View(db.Products.FirstOrDefault(x=>x.Id == id));
        }
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Product product = db.Products.FirstOrDefault(x => x.Id == id);
            try
            {
                UpdateModel(product);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(product);
            }
        }
    }
}
